package com.itlong.cloud.controller.user;

import com.itlong.cloud.POJO.DTO.operate.OperateUserFeedbackPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserFeedbackVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperateUserFeedbackServiceClient;
import com.itlong.cloud.utils.encrypt.DesEncryptUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营—APP用户模块—用户反馈。
 * <desc/>
 *
 * @createDate 2017/10/19.
 */
@RestController
@RequestMapping("/appUser/userFeedback")
public class OperateUserFeedbackController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateUserFeedbackController.class);

    //用户反馈
    @Autowired
    IOperateUserFeedbackServiceClient iOperateUserFeedbackServiceClient;

    /**
     * <desc>
     *      反馈用户信息回复。
     * <desc/>
     *
     * @param  id
     * @param  replyContent 回复内容
     * @return
     * @author shengen.T
     * @createDate 2017/9/25
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.FEEDBACKUSER_REPLY_CONTENT_LOG_DESC)
    @RequestMapping(path="/replyUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object replyUserInfo(String id,String replyContent, Integer clientShowResult) {
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            id= request.getAttribute("id")==null?null:request.getAttribute("id").toString();
            replyContent= request.getAttribute("replyContent")==null?null:request.getAttribute("replyContent").toString();
            clientShowResult = request.getAttribute("clientShowResult") == null ?
                    null : Integer.parseInt(request.getAttribute("clientShowResult").toString());

            if(StringUtils.isBlank(id)){
                return new MessageVO(OperateErrorCodeEnum.USER_ID_EMPTY_ERR.getErrorCode());
            }
            if(StringUtils.isBlank(replyContent)){
                return new MessageVO(OperateErrorCodeEnum.USER_REPLY_CONTENT_EMPTY_ERR.getErrorCode());
            }
            if (clientShowResult == null){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if ( !NumberHandlerUtil.isInRange(clientShowResult, 0, 1)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            int result = iOperateUserFeedbackServiceClient.replyUserInfo(id,replyContent, clientShowResult);
            if(result > 0){
                return  new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
            }else{
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode());
            }
        } catch (Exception e) {
            throw new DataAccessException("【运营_用户_用户反馈】回复反馈用户内容失败:",e);
        }

    }

    public static void main(String[] args) throws Exception {
        //项目公解密过程
        String projectId = "00002345";
        String encryptedPubKey = "8A53228E9B225225";
        System.out.println("项目ID(key):" + projectId + " 项目密钥密文(data)：" + encryptedPubKey);
//        String pubKey = DesEncryptUtil.decode(projectId, encryptedPubKey);
//        System.out.println("解密得到项目密钥:" + pubKey);

        //用户密钥生成过程
        String userId = "12345678";
        String pubKey = "1ACE988CD7E52AF8";
        System.out.println("项目密钥(key):" + pubKey + " 用户ID(data)：" + userId);
        String userKey = DesEncryptUtil.encode(pubKey, userId);
        System.out.println("加密得到用户密钥:" + userKey);


    }

    /**
     * <desc>
     *     获取反馈用户信息列表。
     * <desc/>
     *
     * @param  pageDTO  请求参数
     * @return
     * @author shengen.T
     * @createDate 2017/9/26
     */
    @AuthAccessRequired
    @RequestMapping(path="/getFeedbackUserPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getFeedbackUserPage(OperateUserFeedbackPageDTO pageDTO) {
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO=HttpProtocolUtil.parseRequestParamToDTO(OperateUserFeedbackPageDTO.class,request);
            if(null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateUserFeedbackVO> page= iOperateUserFeedbackServiceClient.getFeedbackUserPage(pageDTO);
            return  new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_用户_用户反馈getPage】获取分页列表失败：",e);
        }
    }


    /**
     * <desc>
     *      处理app用户反馈主键为空的数据
     * </desc>
     *
     * @return
     * @Author Zhu.yj
     * @createDate  2020/11/27
     */
    @UnAuthAccessRequired
    @UnParamEncrypt
    @PostMapping("handlerFeedbackIdIsNull")
    public Object handlerFeedbackIdIsNull(){
        try {
            Integer result = iOperateUserFeedbackServiceClient.handlerFeedbackIdIsNull();

            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        }catch (Exception e){
            throw new DataAccessException("【运营_用户_用户反馈handlerFeedbackIdIsNull】处理app用户反馈主键为空数据失败", e);
        }
    }
}
